package com.atguigu.ssyx.receiver;


import com.atguigu.ssyx.common.constant.MqConst;
import com.atguigu.ssyx.search.service.SkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class SkuReceiver {

    @Autowired
    private SkuService skuService;



    //上架商品消息监听
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(value= MqConst.QUEUE_GOODS_UPPER,durable="true",arguments = @Argument(name="x-queue-mode",value= "lazy")),
            exchange=@Exchange(value= MqConst.EXCHANGE_GOODS_DIRECT),
            key={MqConst.ROUTING_GOODS_UPPER}
    ))
    public void upperSku(Long skuId, Message message, Channel channel) throws IOException {

        log.error("消息接受成功，是:{}",skuId);
        if(skuId!=null){
            skuService.upperSku(skuId);

        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }




    //下架商品消息监听
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(value= MqConst.QUEUE_GOODS_LOWER,durable="true",arguments = @Argument(name="x-queue-mode",value= "lazy")),
            exchange=@Exchange(value= MqConst.EXCHANGE_GOODS_DIRECT),
            key={MqConst.ROUTING_GOODS_LOWER}
    ))
    public void lowerSku(Long skuId, Message message, Channel channel) throws IOException {
        log.error("消息接受成功，是:{}",skuId);
        if(skuId!=null){
            skuService.lowerSku(skuId);

        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }


    //删除商品消息监听
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(value= MqConst.QUEUE_GOODS_DELETE,durable="true",arguments = @Argument(name="x-queue-mode",value= "lazy")),
            exchange=@Exchange(value= MqConst.EXCHANGE_GOODS_DIRECT),
            key={MqConst.ROUTING_GOODS_DELETE}
    ))
    public void deleteSku(Long skuId, Message message, Channel channel) throws IOException {
        log.error("消息接受成功，是:{}",skuId);
        if(skuId!=null){
            skuService.deleteSku(skuId);

        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }


}
